define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            var parent_id = Fast.api.query('parent_id');
            var addUrl = 'district/add';
            if (parent_id > 0) {
                addUrl += '?parent_id=' + parent_id;
            }
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'district/index' + location.search,
                    add_url: addUrl,
                    edit_url: 'district/edit',
                    del_url: 'district/del',
                    multi_url: 'district/multi',
                    table: 'district'
                },
                showExport: false
            });

            var table = $("#table");
            table.on('post-common-search.bs.table', function (event, table) {
                var form = $("form", table.$commonsearch);
                $("input[name='province_id']", form).addClass("selectpage")
                    .data("source", "district/index?level=province")
                    .data("primaryKey", "id")
                    .data("field", "name")
                    .data("orderBy", "id asc");
                $("input[name='city_id']", form).addClass("selectpage")
                    .data("source", "district/index?level=city")
                    .data("primaryKey", "id")
                    .data("field", "name")
                    .data("params", function (obj) {
                        return {custom: { parent_id: 27833}};
                    })
                    .data("orderBy", "id asc");
                $("input[name='area_id']", form).addClass("selectpage")
                    .data("source", "district/index?level=district")
                    .data("primaryKey", "id")
                    .data("field", "name")
                    .data("params", function (obj) {
                        return {custom: { parent_id: 28141}};
                    })
                    .data("orderBy", "id asc");
                Form.events.cxselect(form);
                Form.events.selectpage(form);
            });
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                sortOrder: 'asc',
                columns: [
                    [
                        { field: 'state', checkbox: true, },
                        { field: 'id', title: __('Id') },
                        { field: 'type', title: __('Type'),searchList: typeList, operate: '&'},
                        { field: 'shop_name', title: __('Shop Name'), operate: 'LIKE',
                            hidden:function(value, row, index){
                                if(row.type & 1){
                                    return false;
                                }
                                return true;
                            }
                        },
                        { field: 'name', title: __('Name'), operate: 'LIKE',
                            hidden:function(value, row, index){
                                if(row.type & 2){
                                    return false;
                                }
                                return true;
                            }
                        },
                        { field: 'adcode', title: __('Adcode'), visible: false, operate: false },
                        { field: 'citycode', title: __('Citycode'), visible: false, operate: false },
                        { field: 'level', title: __('Level'), formatter: Table.api.formatter.status, custom: {
                            'country': 'success',
                            'provice': 'success',
                            'city': 'success',
                            'district': 'success',
                            'street': 'success',
                        }, searchList: {
                            'country': __('Level country'),
                            'province': __('Level province'),
                            'city': __('Level city'),
                            'district': __('Level district'),
                            'street': __('Level street'),
                        }, sortable: true, visible: true, operate: false },
                        {
                            field: 'operate', title: __('Operate'), table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'lower_area',
                                    text: '查看下级区域',
                                    icon: 'fa fa-list',
                                    classname: 'btn btn-info btn-xs btn-lower_area addtabsit',
                                    url: 'district/index?parent_id={id}',
                                    hidden: function (row, dom) {
                                        if (row.level == 'street') {
                                            return true;
                                        } else {
                                            return false;
                                        }
                                    }
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
            // 当表格数据加载完成时
            table.on('load-success.bs.table', function (e, data) {
                //这里可以获取从服务端获取的JSON数据
                //console.log(data);
                //这里我们手动设置底部的值
                $(".nav .active a").text(data.title);
            });
        },
        add: function () {
            this.api.bindevent();
        },
        edit: function () {
            this.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
                //Districts.districtCpcdsEvent();
            }
        }
    };
    return Controller;
});